/**
 * Created by LeungZ on 2016/10/17.
 */
import './ser-comp/_components.js';

import temp from './service.html';
import ctrl from './service.ctrl.js';

import dtlTemp from './detail/detail.html';
import dtlCtrl from './detail/detail.ctrl.js';

import './create/create.scss';
import creTemp from './create/create.html';
import creCtrl from './create/create.ctrl.js';

import creBaseTemp from './create/base/base.html';
import creBaseCtrl from './create/base/base.ctrl.js';

import creDeployTemp from './create/deployment/deployment.html';
import creDeployCtrl from './create/deployment/deployment.ctrl.js';

import creReleaseTemp from './create/release/release.html';
import creReleaseCtrl from './create/release/release.ctrl.js';

import creVerTemp from './detail/create/create.html';
import creVerCtrl from './detail/create/create.ctrl.js';

import creVerDeployTemp from './detail/create/deployment/deployment.html';
import creVerDeployCtrl from './detail/create/deployment/deployment.ctrl.js';


export default angular.module('ctg-k8sconsole')
    .config(['$stateProvider', $stateProvider => {
        $stateProvider.state('app-service', {
            url: '/app/service',
            templateUrl: temp,
            controller: ctrl,
            controllerAs: 'list',
            resolve: {
                list: ['appSrv', 'ctgNotification', function(appSrv, ctgNotification) {
                    return appSrv.getAppList({ currentPage: 1 }).catch(function(data) {
                        ctgNotification.error('获取应用列表数据有误.');
                    });
                }],
                config: ['appSrv', function(appSrv) {
                    if (!appSrv._logPath) {
                     return   appSrv.getConfig().then(function(data) {
                            appSrv._logPath = data['log.hostPath'];
                            appSrv._cpuReq = data['cpu.request'];
                            appSrv._memReq = data['memory.request'];
                            appSrv._memUnit = data['memory.unit'];
                            appSrv._dfPort = data['deployment.externalPort'];
                            appSrv._memorylimit = Number(data['memory.limit']);
                            appSrv._cpulimit = Number(data['cpu.limit']);
                        })
                    }
                }]
            }
        }).state('app-service-publish', {
            templateUrl: creTemp,
            controller: creCtrl,
            controllerAs: 'vm',
            abstract: true,
            resolve: {
                appImgList: ['appSrv', 'ctgNotification', '$stateParams', function(appSrv, ctgNotification) {
                    return appSrv.getBuildImages().catch(function(data) {
                        ctgNotification.error('获取应用镜像列表数据有误.');
                    });
                }],
                clusterList: ['clusterSrv', 'ctgNotification', '$stateParams', function(clusterSrv, ctgNotification) {
                    return clusterSrv.getClusterList({ perPage: 99, currentPage: 1 }).catch(function(data) {
                        ctgNotification.error('获取容器集群列表数据有误.');
                    });
                }]
            }
        }).state('app-service-publish.base', {
            url: "/app/service/publish/base",
            templateUrl: creBaseTemp,
            controller: creBaseCtrl,
            controllerAs: 'vm'
        }).state('app-service-publish.deploy', {
            url: '/app/service/publish/deploy',
            templateUrl: creDeployTemp,
            controller: creDeployCtrl,
            controllerAs: 'vm'
        }).state('app-service-publish.release', {
            url: '/app/service/publish/release',
            templateUrl: creReleaseTemp,
            controller: creReleaseCtrl,
            controllerAs: 'vm'
        }).state('app-service-detail', {
            url: '/app/service/:id',
            templateUrl: dtlTemp,
            controller: dtlCtrl,
            controllerAs: 'dtl',
            resolve: {
                detail: ['appSrv', 'ctgNotification', '$stateParams', function(appSrv, ctgNotification, $stateParams) {
                    return appSrv.getAppDetail($stateParams.id).catch(function(data) {
                        ctgNotification.error('获取应用详情数据有误.');
                    });
                }],
                services: ['appSrv', 'ctgNotification', '$stateParams', function(appSrv, ctgNotification, $stateParams) {
                    return appSrv.getServiceOfApp({ id: $stateParams.id, currentPage: 1 }).catch(function(data) {
                        ctgNotification.error('获取应用服务列表数据有误.');
                    });
                }],
                config: ['appSrv', function(appSrv) {
                    if (!appSrv._logPath) {
                     return   appSrv.getConfig().then(function(data) {
                            appSrv._logPath = data['log.hostPath'];
                            appSrv._cpuReq = data['cpu.request'];
                            appSrv._memReq = data['memory.request'];
                            appSrv._memUnit = data['memory.unit'];
                            appSrv._dfPort = data['deployment.externalPort'];
                            appSrv._memorylimit = Number(data['memory.limit']);
                            appSrv._cpulimit = Number(data['cpu.limit']);
                        })
                    }
                }]
            }
        }).state('app-service-version-publish', {
            url: '?k8sCode&appId&serId&lesseeCode',
            templateUrl: creVerTemp,
            controller: creVerCtrl,
            controllerAs: 'vm',
            abstract: true,
            resolve: {
                appImgList: ['appSrv', 'ctgNotification','$rootScope', function(appSrv, ctgNotification,$rootScope) {
                    return appSrv.getBuildImages({currentPage: 1,sysUserCode:JSON.parse(localStorage.getItem('myUser')),imageType:"aa"}).catch(function(data) {
                        ctgNotification.error('获取应用镜像列表数据有误.');
                    });
                }],
                groupList: ['nodeSrv', 'ctgNotification', '$stateParams', function(nodeSrv, ctgNotification, $stateParams) {
                    return nodeSrv.getGroupList({ perPage: 99, page: 1, code: $stateParams.k8sCode }).catch(data => {
                        ctgNotification.error('获取宿主机组详情数据有误');
                    })
                }],
                service: ['appSrv', 'ctgNotification', '$stateParams', function(appSrv, ctgNotification, $stateParams) {
                    if ($stateParams.serId) {
                        return appSrv.getServiceDetail($stateParams.serId).catch(function(data) {
                            ctgNotification.error('获取服务数据有误.');
                        });
                    }
                }]
            }
        }).state('app-service-version-publish.deploy', {
            url: '/app/service/version/publish/deploy',
            templateUrl: creVerDeployTemp,
            controller: creVerDeployCtrl,
            controllerAs: 'service'
        }).state('app-service-version-publish.release', {
            url: '/app/service/version/publish/release',
            templateUrl: creReleaseTemp,
            controller: creReleaseCtrl,
            controllerAs: 'vm'
        })
    }]);